package com.wzq.permission.center.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wzq.permission.center.mapper.MenuMapper;
import com.wzq.permission.center.mapper.RoleMenuMapper;
import com.wzq.permission.center.mapper.UserMapper;
import com.wzq.permission.client.model.MenuTypeEnum;
import com.wzq.permission.client.model.Pager;
import com.wzq.permission.client.model.admin.MenuModel;
import com.wzq.permission.client.service.MenuService;
import com.wzq.permission.client.utils.TreeUtil;

/**
 * 用户服务
 * Project:wzq-permission-center 
 * @Author chenjianfei
 * create Time:2015年12月31日 10：21：30
 *
 */
@Service
@Transactional
public class MenuServiceImpl implements MenuService {
	@Autowired
    private MenuMapper mapper;
	@Autowired
	private RoleMenuMapper roleMenuMapper;
	@Autowired
	private UserMapper userMapper;
	@Value("${qiniu.domain.url}")
	private String qiniuDomainUrl;
	
	@Override
	public void add(MenuModel menu) {
		menu.setMenuType("menu");
		menu.setDeleted("0");
		if(menu.getSerialNum() == null){
			if(menu.getParentId().intValue()==-1){
				menu.setSerialNum(mapper.getMaxSerialNumByApplicationId(menu.getApplicationId(), MenuTypeEnum.MENU.getType(),-1l)+1);
			}else{
				menu.setSerialNum(mapper.getMaxSerialNumByApplicationId(menu.getApplicationId(), MenuTypeEnum.MENU.getType(),menu.getParentId())+1);
			}
		}
		mapper.add(menu);
		List<MenuModel> operateMenus = menu.getChildrens();
		//添加操作
		if(operateMenus!=null && operateMenus.size()>0){
			for(MenuModel operateMenu:operateMenus){
				operateMenu.setParentId(menu.getMenuId());
				mapper.add(operateMenu);
			}
		}
	}

	@Override
	public void update(MenuModel menu) {
		mapper.update(menu);
	}

	@Override
	public void updateBySelective(MenuModel menu) {
		mapper.updateBySelective(menu);
		List<MenuModel> operateMenus = menu.getChildrens();
		//修改操作
		if(operateMenus!=null && operateMenus.size()>0){
			for(MenuModel operateMenu:operateMenus){
				operateMenu.setParentId(menu.getMenuId());
				if(operateMenu.getMenuId()==null){
					mapper.add(operateMenu);
				}else{
					if("0".equals(operateMenu.getDeleted())){
						operateMenu.setUpdateTime(new Date());
						mapper.updateBySelective(operateMenu);
					}else{
						//删除角色菜单关联
						roleMenuMapper.deleteByMenuIds(new Long[]{operateMenu.getMenuId()});
						mapper.delete(operateMenu.getMenuId());
					}
				}
			}
		}
	}

	@Override
	public int queryByCount(MenuModel menu) {
		return mapper.queryByCount(menu);
	}

	@Override
	public List<MenuModel> queryList(MenuModel menu,Pager pager) {
		if(StringUtils.isNotEmpty(menu.getMenuName())){
			menu.setMenuName("%"+menu.getMenuName()+"%");
		}
		return mapper.queryList(menu,pager);
	}

	@Override
	public MenuModel queryById(Object id) {
		return mapper.queryById(id);
	}

	@Override
	public void delete(Long[] menuIds) {
		if(menuIds != null && menuIds.length > 0){
			roleMenuMapper.deleteByMenuIds(menuIds);
			for(Long menuId:menuIds){
				mapper.deleteByParentId(menuId);
				mapper.delete(menuId);
			}
		}
	}

	@Override
	public List<MenuModel> queryMenuByApplicationId(Long applicationId) {
		List<MenuModel> menuList = mapper.queryMenuByApplicationId(applicationId,MenuTypeEnum.MENU.getType());
		return TreeUtil.menuListToTree(menuList);
	}

	@Override
	public List<MenuModel> queryMenuAndBtnByApplicationId(Long applicationId) {
		List<MenuModel> menuList = mapper.queryMenuByApplicationId(applicationId,null);
		return TreeUtil.menuListToTree(menuList);
	}

	@Override
	public List<MenuModel> getOperateMenus(Long menuId) {
		return mapper.getOperateMenus(menuId);
	}

	@Override
	public List<MenuModel> queryMenusByUserId(Long userId, Long applicationId) {
	    List<Long> roleIds = userMapper.queryUserAllRoleId(userId, applicationId);
        List<MenuModel> menuList = mapper.queryMenusByRoleIds(roleIds, MenuTypeEnum.MENU.getType());
        if(menuList!=null && qiniuDomainUrl!=null){
            for(MenuModel menuModel:menuList){
                if(StringUtils.isNotEmpty(menuModel.getIcon())){
                    menuModel.setIcon(qiniuDomainUrl+menuModel.getIcon());
                }
            }
        }
        return TreeUtil.menuListToTree(menuList);
	}

	@Override
	public List<MenuModel> queryPermissionByUserId(Long userId,
			Long applicationId) {
	    List<Long> roleIds = userMapper.queryUserAllRoleId(userId, applicationId);
        List<MenuModel> menuList = mapper.queryMenusByRoleIds(roleIds,null);
        return menuList;
	}
}
